package com.ruoyi.asms.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.asms.domain.AsmsFaultReportTag;
import com.ruoyi.asms.service.IAsmsFaultReportTagService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.SysFile;
import com.ruoyi.common.core.domain.WxMaCodeVo;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.security.annotation.PreAuthorize;
import com.ruoyi.security.service.TokensService;
import com.ruoyi.file.controller.SysFileController;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 资产报修码信息Controller
 *
 * @author yfkj
 * @date 2021-10-21
 */
@RestController
@RequestMapping("/asms/AsmsFaultReportTag")
public class AsmsFaultReportTagController extends BaseController {
    @Autowired
    private IAsmsFaultReportTagService asmsFaultReportTagService;

    @Autowired
    private TokensService tokenService;
//    @Autowired
//    private RemoteFileService remoteFileService;
    @Autowired
    private SysFileController sysFileController;

    /**
     * 查询资产报修码信息列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:AsmsFaultReportTag:list")
    @GetMapping("/list")
    public TableDataInfo list(AsmsFaultReportTag asmsFaultReportTag) {
        startPage();
        List<AsmsFaultReportTag> list = asmsFaultReportTagService.selectAsmsFaultReportTagList(asmsFaultReportTag);
        return getDataTable(list);
    }

    /**
     * 导出资产报修码信息列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:AsmsFaultReportTag:export")
    @Log(title = "资产报修码信息", businessType = BusinessType.EXPORT)
    @PostMapping(value="/export", produces = "application/octet-stream")
    public void export(HttpServletResponse response, AsmsFaultReportTag asmsFaultReportTag) throws IOException {
        List<AsmsFaultReportTag> list = asmsFaultReportTagService.selectAsmsFaultReportTagList(asmsFaultReportTag);
        ExcelUtil<AsmsFaultReportTag> util = new ExcelUtil<AsmsFaultReportTag>(AsmsFaultReportTag.class);
        util.exportExcel(response, list, "资产报修码信息数据");
    }

    /**
     * 获取资产报修码信息详细信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:AsmsFaultReportTag:query")
    @GetMapping(value = "/{faultReportTag}")
    public AjaxResult getInfo(@PathVariable("faultReportTag") Long faultReportTag) {
        return AjaxResult.success(asmsFaultReportTagService.selectAsmsFaultReportTagByFaultReportTag(faultReportTag));
    }

    /**
     * 新增资产报修码信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:AsmsFaultReportTag:add")
    @Log(title = "资产报修码信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AsmsFaultReportTag asmsFaultReportTag) {
        return toAjax(asmsFaultReportTagService.insertAsmsFaultReportTag(asmsFaultReportTag));
    }

    /**
     * 修改资产报修码信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:AsmsFaultReportTag:edit")
    @Log(title = "资产报修码信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AsmsFaultReportTag asmsFaultReportTag) {
        return toAjax(asmsFaultReportTagService.updateAsmsFaultReportTag(asmsFaultReportTag));
    }

    /**
     * 删除资产报修码信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:AsmsFaultReportTag:remove")
    @Log(title = "资产报修码信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{faultReportTags}")
    public AjaxResult remove(@PathVariable Long[] faultReportTags) {
        return toAjax(asmsFaultReportTagService.deleteAsmsFaultReportTagByFaultReportTags(faultReportTags));
    }
    @ApiOperation("获取默认报修码信息")
    @GetMapping("getDefaultReportCodeInfo")
    public R<WxMaCodeVo> getDefaultReportCodeInfo() {


        AsmsFaultReportTag defaultTag = asmsFaultReportTagService.getById(1L);
        WxMaCodeVo wxMaCodeVo = JSONObject.parseObject(defaultTag.getTagInfo(), WxMaCodeVo.class);
        BeanUtils.copyProperties(defaultTag,wxMaCodeVo);
        return R.ok(wxMaCodeVo);
    }
    @ApiOperation("获取部门报修码信息")
    @GetMapping("getDeptReportCodeInfo")
    public R<WxMaCodeVo> getDeptReportCodeInfo() {
        AsmsFaultReportTag tag = asmsFaultReportTagService.getOne(new QueryWrapper<AsmsFaultReportTag>()
                .in(AsmsFaultReportTag.DEPT_ID,tokenService.getUserTopDeptId()),false);
        if(tag==null){
            return R.ok(null);
        }
        WxMaCodeVo wxMaCodeVo = JSONObject.parseObject(tag.getTagInfo(), WxMaCodeVo.class);
        BeanUtils.copyProperties(tag,wxMaCodeVo);
        return R.ok(wxMaCodeVo);
    }
    @ApiOperation("生成报修码信息")
    @PostMapping(value="createWxMaCode",produces = "application/octet-stream")
    public AjaxResult createWxMaCode(@RequestBody WxMaCodeVo vo) {
        String tagInfo = JSONObject.toJSONString(vo);
        System.out.println(tagInfo);
        if(vo.getFaultReportTagId()==null){
            //新增的部门报修码
            AsmsFaultReportTag tag =new AsmsFaultReportTag();
            tag.setDeptId(tokenService.getUserTopDeptId());
            tag.setTagInfo(tagInfo);
            tag.setCreateTime(System.currentTimeMillis());
            tag.setCreateBy(SecurityUtils.getUsername());
            tag.setUpdateTime(System.currentTimeMillis());
            tag.setUpdateBy(SecurityUtils.getUsername());
            asmsFaultReportTagService.save(tag);
        }else {
            AsmsFaultReportTag tag = asmsFaultReportTagService.getById(vo.getFaultReportTagId());
            tag.setUpdateTime(System.currentTimeMillis());
            tag.setUpdateBy(SecurityUtils.getUsername());
            tag.setTagInfo(tagInfo);
            asmsFaultReportTagService.updateById(tag);
        }
       return  success();
    }
    @PostMapping("/uploadImage")
    @ApiOperation("上传图片")
    public R<SysFile> uploadAssetImage(@RequestPart(value = "file") MultipartFile file) {
        R<SysFile> upload = sysFileController.upload(file, "wxMaCode/backImage");
        if (R.FAIL == upload.getCode()) {
            throw new ServiceException(upload.getMsg());
        }
        return upload;
    }
}
